const CopyWebpackPlugin = require('copy-webpack-plugin'); //  复制static文件
const CompressionPlugin = require('compression-webpack-plugin'); //  打包gz

module.exports = {
  css: {
    loaderOptions: {
      less: {
        lessOptions: {
          modifyVars: {
            'primary-color': '#1DA57A',
            'link-color': '#1DA57A',
            'border-radius-base': '2px',
          },
          javascriptEnabled: true,
        },
      },
    },
  },
  chainWebpack: (config) => {
    //  先删掉原来的坑爹的配置的copy
    config.plugins.delete('copy');
  },
  configureWebpack: config => {
    // 处理markdown文件
    config.module.rules.push({
      test: /\.md$/,
      use: [
        {
          loader: "vue-loader"
        },
        {
          loader: require.resolve("./src/common/markdownLoader")
        }
      ],
    });
    //  把文件全给copy到static里面进去
    config.plugins.push(
      new CopyWebpackPlugin({
        patterns: [
          {
            from: 'static',
            to: 'static',
            toType: 'dir',
          },
        ]
      }),
    );
    //  如果是生产环境，生成gz
    if (process.env.NODE_ENV === 'production') {
      config.plugins.push(
        new CompressionPlugin({
          test: /\.(js|css|html)$/,
          threshold: 10240,
          deleteOriginalAssets: false,
        }),
      );
    }
  },
  devServer: {
    port: 5200,
    proxy: {
      '/api': {
        target: '/',
        changeOrigin: true,
        pathRewrite: {
          '^/api': '/',
        },
      },
    },
    hot: true, //  热更新
    open: true, //  自启动
    disableHostCheck: true,
  },
  publicPath: './',
  productionSourceMap: false, // 关闭生成.map文件（.map：未压缩文件知道那行有误，但是文件太大首次加载很慢）
};